package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.domain.NetDisk;
import cn.wolfcode.education.service.INetDiskService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.TreeResult;
import cn.wolfcode.education.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("netDisk")
public class NetDiskController {
    @Autowired
    private INetDiskService netDiskService;
    @Autowired
    private ServletContext context;

    //定位页面
    @RequestMapping("")
    public String index(){
        return "netWorkDisk";
    }

    @RequestMapping("myNetWorkDisk")
    public String indexMyNetDisk(){
        return "myNetWorkDisk";
    }

    //查询出所有的根目录
    @RequestMapping("allParentDisk")
    @ResponseBody
    public TreeResult queryMyNetWorkDisk(){
        TreeResult treeResult = new TreeResult();
        //获取所有的根目录的集合(根目录中包含着它的孩子目录或文件)
        List<NetDisk> netDisks = netDiskService.queryAllParent();
            List<Map<String,Object>> map = new ArrayList<>();
        //创建一个map对象 用来保存rows中的数据
        for (NetDisk netDisk : netDisks) {
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("id", netDisk.getId());
            resultMap.put("name", netDisk.getName());
            resultMap.put("createTime", netDisk.getCreateTime().toLocaleString());
            if (netDisk.getParentId() != null) {
                resultMap.put("_parentId", netDisk.getParentId());
            }
            if (!netDisk.getName().contains(".")) {
                resultMap.put("iconCls", "icon-fileholder");
            } else {
                resultMap.put("iconCls", "icon-file");
            }
            map.add(resultMap);
        }
            treeResult.setRows(map);
            return treeResult;
    }

    //文件上传
    @RequestMapping("postData")
    @ResponseBody
    public JSONResult upload(MultipartFile fileImport,Long parentId,String fileName,String name){
        JSONResult jsonResult = new JSONResult();
        if(name.contains(".")){
            jsonResult.setMsg("该目录为文件,不能再存放文件!");
            return jsonResult;
        }
        //获取到/upload的真实路径
        String path = context.getRealPath("/upload");
        //设置路径
        String upload = UploadUtil.upload(fileImport, path);
        try{
            netDiskService.upload(fileImport,upload,parentId,fileName);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
            return jsonResult;
    }

    //文件下载
    @RequestMapping("downLoadData")
    @ResponseBody
    public JSONResult downLoadData(String filename,String path, HttpServletResponse resp) throws IOException {
        JSONResult jsonResult = new JSONResult();
        //设置下载响应头
        resp.setContentType("application/x-msdownload");
        resp.setHeader("Content-Disposition", "attachment;filename="+filename);

        int index = path.lastIndexOf("/");
        String uuidName = path.substring(index + 1);
        String div = context.getRealPath("/upload");
        Files.copy(Paths.get(div,uuidName),resp.getOutputStream());
            return jsonResult;
    }

    //文件删除
    @RequestMapping("deleteData")
    @ResponseBody
    public JSONResult deleteData(Long id){
        JSONResult jsonResult = new JSONResult();
        try{
            netDiskService.delete(id);
        }catch(Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    //添加文件夹
    @RequestMapping("saveDataFolder")
    @ResponseBody
    public JSONResult saveDataFolder(String fileName,Long id){
        JSONResult jsonResult = new JSONResult();
        try {
            netDiskService.save(fileName,id);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
}
